package servlet.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author Administrator
 * @Date 2020/5/15 8:32
 **/
@WebServlet("/requestDemo3")
public class RequestDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*request对象功能：
        3.获取请求体数据（post请求才有请求体）
         获取输入流对象*/
     /**   BufferedReader br=req.getReader();
        String line=null;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        if (br!=null){
            br.close();
        }
        /*
            获取请求参数
            get请求的请求参数在url里
            post请求的请求参数在请求体里

         */

        String username=req.getParameter("username");
        Map<String ,String[]> parameterMap=req.getParameterMap();
        String[] hobbies=req.getParameterValues("hobby");

        System.out.println("username"+username);
        System.out.println("hobby"+hobbies);
        System.out.println("所有请求的参数Map:");
        Iterator<String> iterator=parameterMap.keySet().iterator();
        while (iterator.hasNext()){
            String key=iterator.next();
            String[] values=parameterMap.get(key);
            System.out.println(key+":"+values.toString());
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}

